package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {

    /**
     * 添加订单
     * @param orders
     */
    @Insert("insert into orders(number, status, user_id, address_book_id, order_time, checkout_time, " +
            "pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, " +
            "rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, " +
            "pack_amount, tableware_number, tableware_status)" +
            "values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}," +
            "#{payMethod}, #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{userName}, #{consignee}," +
            "#{cancelReason}, #{rejectionReason}, #{cancelTime}, #{estimatedDeliveryTime}, #{deliveryStatus}, " +
            "#{deliveryTime}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void insert(Orders orders);

    /**
     * 通过订单名查找订单
     * @param orderNumber
     */
    @Select("select id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, " +
            "amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, " +
            "estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status " +
            "from orders where number = #{orderNumber}")
    Orders selectByorderNumber(String orderNumber);

    /**
     * 修改订单表信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 历史订单查询
     *
     * @param userId
     * @param status
     * @return
     */
    Page<OrderVO> selectOld(Long userId, Integer status, Long id);

    /**
     * 订单搜索
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> selectByPage(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 各个状态的订单数量统计
     * @return
     */
    Integer selectCount(Integer status,LocalDateTime minTime);

    /**
     * 查询派送超时订单
     * @param orders
     * @return
     */
    List<Orders> selectDispatchTimeOut(Orders orders);

    /**
     * 查询每天完成的营业额收入
     * @param status
     * @param minTime
     * @param maxTime
     * @return
     */
    Double getSumMoney(Integer status, LocalDateTime minTime, LocalDateTime maxTime);

    /**
     * 获取订单数列表
     * @param map
     * @return
     */
    Integer getCount(Map<String, Object> map);

    /**
     * 查询商品名称及销量
     * @param map
     * @return
     */
    List<GoodsSalesDTO> top10(Map<String, Object> map);


    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

}
